package com.ch.home.service.serviceImpl.restDishServiceImpl;

import com.ch.home.bean.Dish;
import com.ch.home.bean.DishAndSources;
import com.ch.home.mapper.restDishMapper.DishMapper;
import com.ch.home.service.serviceImpl.sourceServiceImpl.SourceServiceImpl;
import com.ch.home.service.serviceInterface.restDishService.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by CHEN on 2019/8/26.
 */
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SourceServiceImpl sourceService;

    /**
     * 拿到簡略信息
     *
     * @param dish
     * @return
     */
    @Override
    public HashMap getPreview(Dish dish) {
        HashMap<Object, Object> result = new HashMap<>( );
        LinkedList<DishAndSources> value = new LinkedList<>( );

        LinkedList<DishAndSources> dishAndSource = dishMapper.getDishAndSource(dish);

        DishAndSources dish1;

        if (dishAndSource.size( ) > 0) {
            result.put("message", "success");
            for (int i = 0; i < dishAndSource.size( ); i++) {
                dish1 = dishAndSource.get(i);
                System.out.println(dish1.getSourcePreviewUrl( ));
                dish1.setUrl(null);

                if (i >= 1 && dish1.getDishName( ).equals(dishAndSource.get(i - 1).getDishName( ))) {
                    System.out.println("--------------------------------------");
                    continue;
                }
                value.add(dish1);
            }
            result.put("value", value);

        } else {
            result.put("message", "fail");
            result.put("value", null);
        }


        return result;
    }

    @Override
    public HashMap getAll(Dish dish) {
        HashMap<Object, Object> result = new HashMap<>( );
        LinkedList<DishAndSources> value = new LinkedList<>( );
        LinkedList<DishAndSources> dishAndSource = dishMapper.getDishAndSource(dish);
        DishAndSources dish1;
        DishAndSources dish2;
        if (dishAndSource.size( ) > 0) {
            result.put("message", "success");
            for (int i = 0; i < dishAndSource.size( ); i++) {
                dish1 = dishAndSource.get(i);
                if (i >= 1 && dish1.getDishName( ).equals(dishAndSource.get(i - 1).getDishName( ))) {
                    value.getLast().addUrl(dish1.getUrl());
                    System.out.println("--------------------------------------");
                    continue;
                }
                dish1.addUrl(dish1.getUrl());
                dish1.setUrl(null);
                dish1.setSourcePreviewUrl(null);
                value.add(dish1);
                result.put("value", value);
            }
        } else {
            result.put("message", "fail");
            result.put("value", null);
        }
        return result;
    }

    @Override
    public HashMap add(Dish dish) {
        HashMap<Object, Object> result = new HashMap<>( );

        int re = dishMapper.addDish(dish);
        if (re == 1) {
            result.put("message", "success");
        } else {
            result.put("message", "fail");
        }
        return result;
    }

    @Override
    public HashMap update(Dish dish) {
        HashMap<Object, Object> result = new HashMap<>( );

        int re = dishMapper.updateDish(dish);
        if (re == 1) {
            result.put("message", "success");
        } else {
            result.put("message", "fail");
        }
        return result;
    }
}
